package com.myproject.mall.common.handler;

import com.myproject.mall.common.constants.GlobalCode;
import com.myproject.mall.common.exception.GlobalException;
import com.myproject.mall.common.result.HttpResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(GlobalException.class)
    public HttpResult<String> handleGlobalException(GlobalException e) {
        log.error("GlobalException: {}", e.getMessage());
        return HttpResult.fail(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public HttpResult<String> handleException(Exception e) {
        log.error("Other Exception: {}", e.getMessage());
        return HttpResult.fail(GlobalCode.FAIL, e.getMessage());
    }

}
